package com.pennywise.controller;

import com.pennywise.common.response.Result;
import com.pennywise.model.Dictionary;
import com.pennywise.service.DictionaryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "字典管理")
@RestController
@RequestMapping("/api/dictionaries")
@RequiredArgsConstructor
public class DictionaryController {
    
    private final DictionaryService dictionaryService;
    
    @Operation(summary = "添加字典")
    @PostMapping
    public Result<Void> addDictionary(@RequestBody Dictionary dictionary) {
        dictionaryService.addDictionary(dictionary);
        return Result.success(null);
    }
    
    @Operation(summary = "更新字典")
    @PutMapping("/{id}")
    public Result<Void> updateDictionary(@PathVariable Long id, @RequestBody Dictionary dictionary) {
        dictionary.setId(id);
        dictionaryService.updateDictionary(dictionary);
        return Result.success(null);
    }
    
    @Operation(summary = "删除字典")
    @DeleteMapping("/{id}")
    public Result<Void> deleteDictionary(@PathVariable Long id) {
        dictionaryService.deleteDictionary(id);
        return Result.success(null);
    }
    
    @Operation(summary = "获取字典")
    @GetMapping("/{id}")
    public Result<Dictionary> getDictionary(@PathVariable Long id) {
        return Result.success(dictionaryService.getDictionary(id));
    }
    
    @Operation(summary = "获取字典列表")
    @GetMapping("/type/{type}")
    public Result<List<Dictionary>> getDictionariesByType(@PathVariable String type) {
        return Result.success(dictionaryService.getDictionariesByType(type));
    }
    
    @Operation(summary = "搜索字典")
    @GetMapping("/search")
    public Result<List<Dictionary>> searchDictionaries(
            @RequestParam String type,
            @RequestParam(required = false) String keyword) {
        return Result.success(dictionaryService.searchDictionaries(type, keyword));
    }
} 